<?php

class Ftp
{	 
	private $host;
	private $port = 21;
	private $user;
	private $pass;
	private $errNo;
	private $errMsg;
	private $mode = FTP_BINARY; // FTP_ASCII 
	private $passive = true;
	private $timeout = 90;
	private $handler;		
	
	//TODO: Implements all methods	
	public function connect($host = null, $port = null)
	{
		if (isset($host))
			$this->setHost($host);
		
		if (isset($port))
			$this->setPort($port);
			
		$handler = @ftp_connect($this->getHost(), $this->getPort(), $this->getTimeout());
	
		if (!$handler)
		{
			$this->setErrMsg(Languages::get('FtpMessages.ftp_connect_error'));
			return false;
		}
		else
		{
			$this->setHandler($handler);
			return true;
		}	
	}	
	
	public function login($user = null, $pass = null)
	{
		if (isset($user))
			$this->setUser($user);
		
		if (isset($pass))
			$this->setPass($pass);
			
		$login = ftp_login($this->getHandler(),$this->getUser(),$this->getPass());
		
		if (!$login)
		{
			$this->setErrMsg(Languages::get('FtpMessages.ftp_login_error'));
			return false;	
		}
		else
			return true;		
	}
	
	public function disconnect()
	{}
		
	public function isValidDir($dir,$type)
	{
		switch($type)
		{
			case "local":
				if (is_dir($dir))
					return true;
				else
					return false;
			break;
			case "remote":
				if ( substr($dir,0,1) == "d" )
					return true;
				else
					return false;
			break;	
		}
	}
	
	public function isValidFile($file)
	{}
	
	public function mkdir($name)
	{}
	
	public function rename($oldName, $newName)
	{}
	
	public function copy($from, $to)
	{}
	
	public function cut($from, $to)
	{}
	
	public function rmdir($name)
	{}
	
	public function delete($name)
	{}
	
	public function cd($path)
	{}
	
	public function ls($path)
	{
		$ftp_rawlist = @ftp_rawlist($this->getHandler(), $path);
		foreach ($ftp_rawlist as $v) 
		{
			$info = array();
			$vinfo = preg_split("/[\s]+/", $v, 9);
			$type = ($this->isValidDir($vinfo['0'], "remote")) ? "dirs" : "files";			
			
			if ($vinfo['0'] !== "total") 
			{
				$info['chmod'] = $vinfo['0'];
				$info['num'] = $vinfo['1'];
				$info['owner'] = $vinfo['2'];
				$info['group'] = $vinfo['3'];
				$info['size'] = $vinfo['4'];
				$info['month'] = $vinfo['5'];
				$info['day'] = $vinfo['6'];
				$info['time'] = $vinfo['7'];
				$info['name'] = $vinfo['8'];
				$rawlist[$type][$info['name']] = $info;
			}
		}
		
		
		
		return $rawlist;
	}
	
	public function rmdirRecursive($path)
	{}
	
	public function chmod($name,$chmod)
	{}
	
	public function listRemoteDirs($path)
	{}	
	
	public function listRemoteFiles($path)
	{}
	
	public function listLocalDirs($path)
	{}
	
	public function listLocalFiles($path)
	{}
	
		
	/*
	*
	* Getters and Setters
	*
	*/
	public function getHost()
	{
		return $this->host;
	}
	
	public function setHost($host)
	{
		$this->host = $host;
	}
	
	public function getPort()
	{
		return $this->port;
	}
	
	public function setPort($port)
	{
		$this->port = $port;
	}
	
	public function getUser()
	{
		return $this->user;
	}
	
	public function setUser($user)
	{
		$this->user = $user;
	}
	
	public function getPass()
	{
		return $this->pass;
	}
	
	public function setPass($pass)
	{
		$this->pass = $pass;
	}	
		
    public function getErrno()
    {
    	return $this->errNo;
    }	
    
    public function setErrNo($errNo)
    {
    	$this->errNo = $errNo;
    }    
    
    public function getErrMsg()
    {
    	return $this->errMsg;
    }
    
    public function setErrMsg($errMsg)
    {
    	$this->errMsg = $errMsg;
    }
    
    public function setMode($mode)
    {
    	$this->mode = $mode;
    }
    
    public function getMode()
    {
    	return $this->mode;
    }
	
	public function setPassive()
	{
		$this->passive = true;
	}
	
	public function setActive()
	{
		$this->passive = false;
	}
	
	public function isPassive()
	{
		return $this->passive;
	}
	
	public function setTimeout($timeout)
	{
		$this->timeout = $timeout;
	}
	
	public function getTimeout()
	{
		return $this->timeout;	
	}
	
	public function setHandler($handler)
	{
		$this->handler = $handler;
	}
	
	public function getHandler()
	{
		return $this->handler;
	}    
	 
}

?>
